'''
Author: cw
Date: 2025-07-21
Description: ...
'''

from sqlalchemy import select

from models.mysql_model import Questions
from utils import db_util


async def get(test_id: int, offset: int) -> Questions:
    """info"""
    async with db_util.session_context() as session:
        question = (await session.execute(
            select(Questions).where(
                Questions.test_id == int(test_id),
            ).order_by(Questions.id).offset(offset)
        )).scalar()
        return question


async def build(test_id: int, offset: int = 0):
    """构建问题"""
    condition = [
        Questions.test_id == int(test_id)
    ]

    async with db_util.session_context() as session:
        question = (await session.execute(
            select(Questions).filter(
                *condition
            ).order_by(Questions.id).offset(offset)
        )).scalar()
        if question:
            # 构建问题消息
            options_text = "\n".join([
                f"{item.get('key')}.{item.get('value')}"
                for item in question.options
            ])
            return f"问题 {(offset + 1)}: {question.question_title}\n\n{options_text}\n\n请回复选项字母(A、B、C、D)"
